import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///图片加载工具类
class NetImageLoadUtils {
  NetImageLoadUtils._();

  ///加载网络图片
  static Widget load(
    String? imageUrl,
    double? width,
    double? height, {
    Color? tintColor,
    BoxFit fit = BoxFit.cover,
    bool isSetRadius = false,
    bool isSetBorder = false,
    bool showLoadWidget = false,
    double borderWidth = 0.6,
    Color borderColor = Colors.white,
    double radius = 0,
    BorderRadius? borderRadius,
    Color placeholderColor = const Color(0xffebebeb),
    Widget? errorWidget,
  }) {
    String url = '';
    if (imageUrl?.isNotEmpty == true) {
      url = imageUrl!;
    }
    CachedNetworkImage cachedNetworkImage = CachedNetworkImage(
      imageUrl: url,
      width: width,
      height: height,
      fit: fit,
      color: tintColor,
      placeholder: (context, url) => showLoadWidget
          ? CupertinoActivityIndicator(
              radius: 16.w,
              color: Colors.grey,
            )
          : Container(
              color: placeholderColor,
              alignment: Alignment.center,
            ),
      errorWidget: (context, url, error) =>
          errorWidget ??
          Container(
            color: placeholderColor,
            alignment: Alignment.center,
          ),
    );

    return Container(
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
        borderRadius: isSetRadius
            ? borderRadius ?? BorderRadius.all(Radius.circular(radius))
            : null,
        border: isSetBorder
            ? Border.all(color: borderColor, width: borderWidth)
            : null,
      ),
      child: cachedNetworkImage,
    );
  }

  ///加载本地图片
  static Widget loadByFile(
    String? imagePath,
    double? width,
    double? height, {
    BoxFit fit = BoxFit.cover,
    bool isSetRadius = false,
    bool isSetBorder = false,
    double borderWidth = 0.6,
    Color borderColor = Colors.white,
    double radius = 0,
    Color placeholderColor = const Color(0xffebebeb),
  }) {
    File? imageFile;
    if (imagePath?.isNotEmpty == true) {
      imageFile = File(imagePath!);
    }

    return Container(
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
        borderRadius:
            isSetRadius ? BorderRadius.all(Radius.circular(radius)) : null,
        border: isSetBorder
            ? Border.all(color: borderColor, width: borderWidth)
            : null,
      ),
      child: imageFile != null
          ? Image.file(
              imageFile,
              width: width,
              height: height,
              fit: fit,
            )
          : Container(
              color: placeholderColor,
              alignment: Alignment.center,
            ),
    );
  }

  ///加载来自网络的圆角
  static Widget loadCircle(
    String? imageUrl,
    double size, {
    String? holderImg,
    BoxFit fit = BoxFit.cover,
    bool isSetBorder = false,
    double borderWidth = 0.6,
    Color borderColor = Colors.white,
    Color placeholderColor = const Color(0xffebebeb),
    Widget? errorWidget,
  }) {
    String url = '';
    if (imageUrl?.isNotEmpty == true) {
      url = imageUrl!;
    }
    var realSize = size - (isSetBorder ? borderWidth : 0);
    return Container(
        clipBehavior: Clip.antiAlias,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(size / 2),
          border: isSetBorder
              ? Border.all(color: borderColor, width: borderWidth)
              : null,
        ),
        child: CachedNetworkImage(
          width: realSize,
          height: realSize,
          imageUrl: url,
          imageBuilder: (context, imageProvider) => Container(
            decoration: BoxDecoration(
              image: DecorationImage(image: imageProvider, fit: fit),
              borderRadius: BorderRadius.circular(size / 2),
            ),
          ),
          placeholder: (context, url) => Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(realSize / 2),
              color: placeholderColor,
            ),
          ),
          errorWidget: (context, url, error) =>
              errorWidget ??
              Container(
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(realSize / 2),
                  color: placeholderColor,
                ),
              ),
        ));
  }

  static Widget loadCircleByFile(
    String? imagePath,
    double size, {
    String? holderImg,
    bool isCircle =
        true, //是否为圆形；true：圆形 false：四边圆角矩形，此时size代表圆角弧度，width和height必须设置
    BoxFit fit = BoxFit.cover,
    bool isSetBorder = false,
    double borderWidth = 0.6,
    Color borderColor = Colors.white,
    Color placeholderColor = const Color(0xffebebeb),
  }) {
    File? imageFile;
    if (imagePath?.isNotEmpty == true) {
      imageFile = File(imagePath!);
    }

    return Container(
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
        borderRadius: isCircle
            ? BorderRadius.circular(size / 2)
            : BorderRadius.all(Radius.circular(size)),
        border: isSetBorder
            ? Border.all(color: borderColor, width: borderWidth)
            : null,
        color: placeholderColor,
      ),
      child: imageFile != null
          ? Image.file(
              imageFile,
              width: size,
              height: size,
              fit: fit,
            )
          : null,
    );
  }

  static ImageProvider? getNetImageProvider(String? imageUrl) {
    String url = '';
    if (imageUrl?.isNotEmpty == true) {
      url = imageUrl!;
    } else {
      return null;
    }
    return CachedNetworkImageProvider(url);
  }
}
